package com.yc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Diary;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface DiaryDao extends BaseMapper<Diary> {
    
    /**
     * 根据用户ID分页查询日记
     */
    @Select("SELECT * FROM diary WHERE userId = #{userId} ORDER BY createTime DESC")
    IPage<Diary> selectByUserId(Page<Diary> page, @Param("userId") Long userId);
    
    /**
     * 根据用户ID和时间范围查询日记
     */
    @Select("SELECT * FROM diary WHERE userId = #{userId} " +
            "AND createTime >= #{startTime} AND createTime <= #{endTime} " +
            "ORDER BY createTime DESC")
    List<Diary> selectByUserIdAndTimeRange(@Param("userId") Long userId, 
                                          @Param("startTime") LocalDateTime startTime, 
                                          @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据用户ID统计日记数量
     */
    @Select("SELECT COUNT(*) FROM diary WHERE userId = #{userId}")
    int countByUserId(@Param("userId") Long userId);
}